#include "bits/stdc++.h"
using namespace std;
#define int long long
void solve()
{
int n, k, pos;
cin >> n >> k >> pos;
vector<int> a(n), b(k);
for (int &x : a)
cin >> x;
for (int &x : b)
cin >> x;
sort(a.begin(), a.end());
sort(b.begin(), b.end());
auto check = [&](int mx)
{
vector<int> vis(k);
// left
for(int i = 0; i < n; ++i)
{
if(a[i] >= pos) break;
int ans = -1;
for(int j = 0; j < k; ++j)
{
if(vis[j]) continue;
int dis = max(0LL, a[i] - b[j]);
if(b[j] > pos) dis = b[j] - pos;
if(2 * dis + pos - a[i] <= mx)
{
ans = j;
break;
}
}
if(ans == -1) return false;
vis[ans] = 1;
}
// right
for(int i = n - 1; i >= 0; --i)
{
if(a[i] < pos) break;
int ans = -1;
for(int j = k - 1; j >= 0; --j)
{
if(vis[j]) continue;
int dis = max(0LL, b[j] - a[i]);
if(b[j] < pos) dis = pos - b[j];
if(2 * dis + a[i] - pos <= mx)
{
ans = j;
break;
}
}
if(ans == -1) return false;
vis[ans] = 1;
}
return true;
};
int lo = 0, hi = 2e9, md, ans = -1;
while(lo <= hi)
{
md = (lo + hi) >> 1;
if(check(md))
hi = md - 1, ans = md;
else
lo = md + 1;
}
assert(ans != -1);
cout << ans << '\n';
}
int32_t main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int tc = 1;
// cin >> tc;
for (int i = 1; i <= tc; ++i)
solve();
}
1217. Minimum Cost to Move Chips to The Same Position | 347. Top K Frequent Elements |
1503. Last Moment Before All Ants Fall Out of a Plank | 430. Flatten a Multilevel Doubly Linked List |
1290. Convert Binary Number in a Linked List to Integer | 1525. Number of Good Ways to Split a String |
72. Edit Distance | 563. Binary Tree Tilt |
1306. Jump Game III | 236. Lowest Common Ancestor of a Binary Tree |
790. Domino and Tromino Tiling | 878. Nth Magical Number |
2099. Find Subsequence of Length K With the Largest Sum | 1608A - Find Array |
416. Partition Equal Subset Sum | 1446. Consecutive Characters |
1618A - Polycarp and Sums of Subsequences | 1618B - Missing Bigram |
938. Range Sum of BST | 147. Insertion Sort List |
310. Minimum Height Trees | 2110. Number of Smooth Descent Periods of a Stock |
2109. Adding Spaces to a String | 2108. Find First Palindromic String in the Array |
394. Decode String | 902. Numbers At Most N Given Digit Set |
221. Maximal Square | 1200. Minimum Absolute Difference |
1619B - Squares and Cubes | 1619A - Square String |